import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class ButtonListener implements ActionListener, ChangeListener {

    RobotPanel robot;
    ControlPanel control;

    MouseClick click;
    // RobotMouseClick robot_click;

    // Add Robot Popup
    GridLayout grid;
    JPanel robot_contentPane;
    JPopupMenu addRobotPanel;

    // Add Light Menu
    JPopupMenu addBlockPanel;
    JComboBox blockSize;
    JPanel block_contentPane;

    public ButtonListener(RobotPanel rob, ControlPanel con) {
        robot = rob;
        control = con;

        click = new MouseClick(robot, control, this);

        // Create Add Robot PopUp
        grid = new GridLayout(0, 1);
        robot_contentPane = new JPanel(grid);

        addRobotPanel = new JPopupMenu();
        robot_contentPane.setVisible(false);
        control.gbl.setConstraints(robot_contentPane, control.gbconst);
        control.add(robot_contentPane);

        // Create Add Light PopuP
        addBlockPanel = new JPopupMenu();
        String[] blockStrings = { "200x200", "150x150", "100x100" };
        blockSize = new JComboBox(blockStrings);
        blockSize.setSelectedIndex(0);
        // blockSize.addActionListener(this);

        block_contentPane = new JPanel(new GridLayout(0, 1));
        block_contentPane.add(new JLabel("Choose Block:"));
        block_contentPane.add(blockSize);
        block_contentPane.add(new JLabel(""));
        block_contentPane.add(new JLabel(""));
        block_contentPane.setVisible(false);
        control.gbl.setConstraints(block_contentPane, control.gbconst);
        control.add(block_contentPane);

        robot.addMouseListener(click);
    }

    public int getBlockSize() {
        if (blockSize.getSelectedIndex() == 0)
            return 200;
        else if (blockSize.getSelectedIndex() == 1)
            return 150;
        else if (blockSize.getSelectedIndex() == 2)
            return 100;
        else
            return 100;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Add Robot")) {
            // System.out.println("Added a Robot to the Simulation");
            // show pop up menu asking for K matrix, initial position, and size

            robot.draw_lines = false;
            control.add_block = false;
            control.add_robot = true;
            robot_contentPane.setVisible(true);
            block_contentPane.setVisible(false);

            control.repaint();
            robot.repaint();

        } else if (e.getActionCommand().equals("Add Block")) {
            // System.out.println("Added a Light to the Simulation");
            // prompt user to click on the screen

            robot.draw_lines = false;
            control.add_robot = false;
            control.add_block = true;
            block_contentPane.setVisible(true);
            robot_contentPane.setVisible(false);

            control.repaint();
            robot.repaint();

        } else if (e.getActionCommand().equals("Create Paths")) {
            robot.CreatePaths();
            robot.repaint();
        } else if (e.getActionCommand().equals("Clear")) {
            robot.draw_lines = false;
            robot.clear();
            robot.repaint();
        } else if (e.getActionCommand().equals("Draw Lines")) {
            robot.draw_lines = !robot.draw_lines;
            control.add_block = false;
            control.add_robot = false;
            robot_contentPane.setVisible(false);
            block_contentPane.setVisible(false);

            robot.compute_lines();

            robot.repaint();
            control.repaint();
        } else {
            System.out.println("Some other button was pushed: "
                    + e.getActionCommand());
        }

    }

    @Override
    public void stateChanged(ChangeEvent arg0) {
        /*
         * JSlider source = (JSlider)arg0.getSource(); if
         * (!source.getValueIsAdjusting()) { if (source.getMaximum() == 360) {
         * int angle = source.getValue(); theta = angle * (Math.PI/180); } else
         * if (source.getMaximum() == 100) { intensity = source.getValue(); } }
         */
    }
}
